<template>
    <div class="message_wrapper">
        <div class="customer_wrap">
            <div class="customer_item" :class="{'active': sender_id == item.sender_id}" v-for="item in tablist" :key="item.id"  @click="clickUser(item.sender_id)">
                <img class="avatar" :src="item.sender_avatar" @error="userErrorPicture(item.id)" width="35" height="35" /> {{ item.sender_nickname }}
                <span :class="{ ts: !item.isflag }"></span>
            </div>
        </div>
        <div class="chat_wrap">
            <div class="chat_content" :class="{'active': isActive}" @click="isActive = false">
                <el-link class="view_more" :underline="false" v-show="show" @click="fy">点击查看更多</el-link>
                <div class="chat_list">
                    <div class="chat_item" v-for="item in list" :key="item.id">
                        <div class="chat-model-left" v-if="item.isself > 0">                            
                            <img class="user_avatar" :src="item.sender_avatar" @error="tabErrorPicture(item.id)" v-if="item.sender_avatar" />
                            <img class="user_avatar" :src="defaultAvatar" v-else />
                            <div class="chat_message">
                                <div class="customer">                                    
                                    <span class="time">{{ item.send_time }}</span>
                                </div>
                                <div class="message" v-html="item.content"></div>
                            </div>                            
                        </div>
                        <div class="chat-model-right" v-else>
                            <img class="user_avatar" :src="item.sender_avatar" @error="tabErrorPicture(item.id)" v-if="item.sender_avatar" />
                            <img class="user_avatar" :src="defaultAvatar" v-else />
                            <div class="chat_message">
                                <div class="customer">                                      
                                    <span class="time">{{ item.send_time }}</span>
                                </div>
                                <div class="message" v-html="item.content"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <el-link class="view_more"  v-show="latelyshow" @click="lately">点击查看最近消息</el-link>
            </div> 
            <div class="chat_footer" :class="{'active': isActive}">
                <div class="chat_send">
                    <div class="user_input">
                        <el-input v-model="text" @focus="isActive = false"></el-input>
                        <el-button @click="onSend">发送</el-button>
                    </div>                    
                    <el-button class="add_btn" icon="el-icon-circle-plus-outline" @click="isActive = !isActive"></el-button>
                </div>
                <div v-show="isActive" class="chat_tools">
                    <div class="tools_item" v-for="item in toolList" :key="item.name">
                        <i :class="item.name"></i>
                        <span class="name">{{ item.title }}</span>
                    </div>
                </div>
            </div>
        </div> 
    </div>
</template>
  
<script>
import { getUserChatList, getChatContent, sendUserChat, updateUserChatFlag } from '../../api/index';
export default {
    name: 'HelloWorld',
    data() {
        return {
            latelyshow: false,
            show: false,
            tablist: [],
            tabPosition: 'left',
            toolList: [
                { name: 'el-icon-picture', title: '图片' },
                { name: 'el-icon-s-goods', title: '商品详情' }
            ],
            defaultAvatar: require('@/assets/img/avatar.png'),
            text: '',
            list: [],
            stime: '', // 定时器
            isActive: false,
            sender_id: '',
            kefu_id: '',
            limit: 10,
            page: 1,
            total: 0,
            messagenumber: 10
        };
    },
    methods: {
        async clickUser(sender_id) {
            this.sender_id = sender_id;
            this.isActive = false;
            await this.getData();
            let res0 = await updateUserChatFlag({ sender_id });
            if (res0.code == 1) {
                this.getList();
            }
            clearInterval(this.stime);
            this.send();
            if (this.total > this.messagenumber) {
                this.show = true;
            }
        },
        async getData() {
            let res = await getChatContent({ sender_id: this.sender_id, limit: this.limit, page: this.page });
            console.log(res);
            if (res.code == 1) {
                this.list = res.data.chatdata.reverse();
                this.total = res.data.total;
            }
        },
        async getUserChatList(){
            let res = await getUserChatList();
            console.log(res);
            if (res.code == 1) {
                this.tablist = res.data.chatlist;
                this.clickUser(this.tablist[0].sender_id)
            }
            
        },
        async getList() {
            let res = await getUserChatList();
            if (res.code == 1) {
                this.tablist = res.data.chatlist;
            }
        },
        async onSend() {
            if (!this.text) {
                this.$toast('请输入内容');
                return;
            }
            this.isActive = false;
            var data = {
                sender_id: this.kefu_id,
                reply_id: this.sender_id,
                content: this.text,
                isself: 0
            };
            this.list.push(data);
            let res = await sendUserChat(data);
            if (res.code == 1) {
                this.text = '';
                let msg = document.getElementById('content'); // 获取对象
                msg.scrollTop = msg.scrollHeight; // 滚动高度
            }
            this.getData();
        },
        send() {
            this.stime = setInterval(() => {
                this.getList();
                this.getData();
            }, 5000);
        },
        async fy() {
            this.page++;
            this.latelyshow = true;
            let res = await getChatContent({ orderid: this.orderid, limit: this.limit, page: this.page });
            if (res.code == 1) {
                this.list = res.data.chatdata.reverse();
                this.total = res.data.total;
            }
            if (this.list.length < this.messagenumber) {
                this.show = false;
            }
        },
        async lately() {
            this.page = 1;
            this.latelyshow = false;
            this.show = true;
            let rst = await getChatContent({ orderid: this.orderid, limit: this.limit, page: this.page });
            if (rst.code == 1) {
                this.list = rst.data.chatdata.reverse();
                this.total = rst.data.total;
            }
        },
        userErrorPicture(id){   // 用户列表默认替换图片
            let i = this.tablist.findIndex(v => v.id == id);
            this.tablist[i].sender_avatar = this.defaultAvatar;
        },
        tabErrorPicture(id){ // 聊天列表默认替换图片
            let i = this.list.findIndex(v => v.id == id);
			this.list[i].sender_avatar = this.defaultAvatar;
        },
    },
    async created() {
        this.kefu_id = this.$route.query.id;
        this.getUserChatList();
    },
    destroyed() {
        // 销毁监听
        clearInterval(this.stime);
    }
};
</script>
  
<style lang="scss" scoped>
.message_wrapper {
    display: flex;
    height: 100%;
    background-color: #fff;
    ::-webkit-scrollbar {
        width: 5px;
        background-color: #aeaeae;
    }
    .customer_wrap{
        width: 200px;
        height: 100%;
        padding: 0 20px;
        box-sizing: border-box;
        border-right: 1px solid #eee;
        .customer_item{
            display: flex;
            align-items: center;
            padding: 15px 0;
            font-size: 14px;
            border-bottom: 1px solid #e4e7ed;
            -webkit-user-select: none;
            user-select: none;
            position: relative;
            cursor: pointer;
            &:hover{
                color: #409EFF;
            }
            .avatar{
                display: block;
                width: 35px;
                height: 35px;
                border-radius: 50%;
                margin: 0 10px 0 0;
            }
        }
        .customer_item.active{
            color: #409EFF;
            &::before{
                content: '';
                width: 2px;
                height: 40px;
                background-color: #409EFF;
                position: absolute;
                right: -21px;
                top: 15px;
            }
        }
    }
    .chat_wrap{
        width: calc(100% - 200px);
        height: 100%;
        background-color: #f1f1f3;       
        position: relative;
        .chat_content{
            padding: 0 20px;
            height: calc(100% - 60px);
            box-sizing: border-box;
            overflow-y: auto;
            .view_more{
                display: block;
                padding: 5px 0 15px;
                text-align: center;
            }
            .chat_list{
                .chat_item{
                    margin: 0 0 15px 0;
                    .chat-model-left{
                        text-align: left;
                        width: 100%;
                        position: relative;
                        padding: 0 0 0 50px;
                        box-sizing: border-box;
                        .user_avatar{
                            width: 45px;
                            height: 45px;
                            border-radius: 50%;
                            position: absolute;
                            left: 0;
                            top: 0;
                        }
                        .chat_message{
                            .customer{
                                display: flex;
                                align-items: center;
                                margin: 0 0 10px 0;
                                .nickname{
                                    font-size: 10px;
                                }
                                .time{
                                    font-size: 11px;
                                    color: #a9a9a9;
                                    // margin: 0 0 0 10px;
                                }
                            }
                            .message{
                                display: inline-block;
						        max-width: 100%;
                                font-size: 13px;	
                                text-align: left;
                                word-break: break-all;
                                word-wrap: break-word;
                                background-color: #fff;                               
                                padding: 5px;
                                border-radius: 3px;
                                box-sizing: border-box;
                                ::v-deep img{
                                    display: block;
                                    max-width: 100%;
                                    max-height: 200px;                                    
                                }
                            }
                        }
                    }
                    .chat-model-right{
                        text-align: right;
                        width: 100%;
                        position: relative;
                        padding: 0 50px 0 0;
                        box-sizing: border-box;
                        .user_avatar{
                            width: 45px;
                            height: 45px;
                            border-radius: 50%;
                            position: absolute;
                            right: 0;
                            top: 0;
                        }
                        .chat_message{
                            .customer{
                                display: flex;
                                justify-content: right;
                                align-items: center;
                                margin: 0 0 10px 0;
                                .nickname{
                                    font-size: 10px;
                                    text-align: right;
                                }
                                .time{                                    
                                    font-size: 11px;
                                    color: #a9a9a9;
                                    // margin: 0 0 0 10px;
                                }
                            }
                            .message{
                                display: inline-block;
						        max-width: 100%;
                                font-size: 13px;	
                                text-align: right;
                                word-break: break-all;
                                word-wrap: break-word;
                                background-color: #95ec69;                               
                                padding: 5px;
                                border-radius: 3px;
                                box-sizing: border-box;
                                ::v-deep img{
                                    display: block;
                                    max-width: 100%;
                                    max-height: 200px;                                    
                                }
                            }
                        }
                    }
                }
            }
        }
        .chat_content.active{
            height: calc(100% - 140px);
        }
        .chat_footer{
            width: 100%;
            height: 58px;
            background-color: #fff;
            position: absolute;
            left: 0;
            bottom: 0;
            .chat_send{
                margin: 10px 10px 0;
                height: 40px;
                display: flex;
                align-items: center;
                .user_input{
                    display: flex;
                    width: 80%;
                    button{
                        margin: 0 10px;
                        background-color: #f59e01;
                        border-color: #f59e01;
                        color: #fff;
                    }
                }
                .add_btn{
                    border: none;
                    padding: 0;
                    font-size: 30px;
                    &:hover{
                        background-color: #fff;
                    }
                    &:focus{
                        background-color: #fff;
                    }
                }
            }
            .chat_tools{
                display: flex;
                flex-wrap: wrap;
                background-color: #fff;
                .tools_item{
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    padding: 10px 20px;
                    box-sizing: border-box;
                    cursor: pointer;
                    i{                
                        font-size: 40px;
                        text-align: center;
                        color: #ccc;
                    }
                    .name{
                        display: block;
                        font-size: 12px; 
                        color: darkgray;
                        margin: 4px auto 0;
                    }
                }
            }
        }
        .chat_footer.active{
            height: 138px;
        }
    }
}
</style>